package com.tencent.sr.iris.activity.service.event.activity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tencent.sr.iris.activity.dependency.user.UserCoreDependency;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityCheckResultDTO;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityDTO;
import com.tencent.sr.iris.activity.domain.event.dto.ActivityTaskDTO;
import com.tencent.sr.iris.activity.interfaces.event.dto.ActivityEventDTO;
import com.tencent.sr.iris.activity.service.config.ActivityConfig;
import com.tencent.sr.iris.activity.service.event.task.IrisTaskExecutor;
import com.tencent.sr.iris.activity.service.event.task.UserLevelUpFosterTaskExecutor;
import com.tencent.sr.iris.user.core.service.interfaces.enums.StaffFromTypeEnum;
import com.tencent.sr.iris.user.core.service.interfaces.enums.UserLevelEnum;
import com.tencent.sr.iris.user.core.service.interfaces.request.message.UserActivityLevelUpRequest;
import com.tencent.sr.iris.user.core.service.interfaces.response.activity.SupervisorRelationResponse;
import com.tencent.sr.rmall.springbootstarter.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 升级有礼(培育奖)
 * 注：当前活动处理器的对象名称是 活动类型ActivityEventTypeEnum的code
 *
 * @author wruiiwang
 * @date 2023/2/17
 */
@Slf4j
@Component
public class UserLevelUpFosterActivityExecutor extends AbstractIrisActivityExecutor {

    @Resource
    private UserLevelUpFosterTaskExecutor userLevelUpFosterTaskExecutor;

    @Resource
    private ActivityConfig activityConfig;

    @Resource
    private UserCoreDependency userCoreDependency;


    @Override
    protected void process(ActivityEventDTO msg, ActivityDTO activity, List<String> eventHandingInfoList) {
        ActivityCheckResultDTO activityFlag = doActivityCheck(activity, msg);
        if (!activityFlag.isSatisfy()) {
            eventHandingInfoList.add("活动不满足准入规则");
            log.info("不满足活动准入规则,activityId={},msg={}", activity, JSONObject.toJSONString(msg));
            return;
        }
        // 循环执行所有任务
        String eventInfo = msg.getEventInfo();
        UserActivityLevelUpRequest levelUpRequest = JSON.parseObject(eventInfo, UserActivityLevelUpRequest.class);
        if (Objects.isNull(levelUpRequest) || StringUtils.isEmpty(levelUpRequest.getAfterLevelId())) {
            log.info("消息体为空 activity:{} msg:{}", JsonUtils.toJsonString(activity), JsonUtils.toJsonString(msg));
            return;
        }
        if (UserActivityLevelUpRequest.PROMO_BLACK.equals(levelUpRequest.getInfo())) {
            log.info("培育奖命中黑名单 activity:{} msg:{}", JsonUtils.toJsonString(activity), JsonUtils.toJsonString(msg));
            eventHandingInfoList.add("培育奖命中黑名单");
            return;
        }
        SupervisorRelationResponse response = userCoreDependency.queryUserSupervisor(msg.getUid());
        if (Objects.nonNull(response) && Objects.nonNull(response.getParentType())
                && StringUtils.isNotBlank(response.getParentUid())) {
            if (!StaffFromTypeEnum.isOuterSign(response.getParentType().getCode())) {
                eventHandingInfoList.add("上级不满足规则," + JsonUtils.toJsonString(response));
                log.info("用户上级为空不满足活动准入规则,activityId={},msg={}", activity, JSONObject.toJSONString(msg));
                return;
            }
        }
        // 获取任务执行器
        activity.getActivityTaskDTOS().forEach(t -> {
            IrisTaskExecutor taskExecutor = getTaskExecutor(msg, activity, t);
            taskExecutor.execute(msg, activity, t, null);
        });

        activityRecordService.addOrUpdateActivityRecord(msg.getUid(), msg.getEventTime(), activity.getBizId(),
                activity.getPeriodType(), activity.getPeriod());
    }

    @Override
    public ActivityCheckResultDTO doActivityCheck(ActivityDTO activity, ActivityEventDTO msg) {
        String eventInfo = msg.getEventInfo();
        UserActivityLevelUpRequest levelUpRequest = JSON.parseObject(eventInfo, UserActivityLevelUpRequest.class);
        if (Objects.isNull(levelUpRequest)) {
            return new ActivityCheckResultDTO(false, "请求体为空");
        }
        // 只有一级级升的才有奖励金
        if (checkActivityCheck(levelUpRequest)) {
            return new ActivityCheckResultDTO(true, "");
        } else {
            return new ActivityCheckResultDTO(false, eventInfo);
        }
    }

    /**
     * 培育奖只在下级由见习升级至初级时才会给上级发
     *
     * @param request
     * @return
     */
    private boolean checkActivityCheck(UserActivityLevelUpRequest request) {
        if (!UserLevelEnum.MEMBER.getValue().equals(request.getAfterLevelId())) {
            return false;
        }
        boolean b = UserLevelEnum.judgeNextLevel(request.getBeforeLevelId(), request.getAfterLevelId());
        return b;
    }


    @Override
    protected void after(ActivityEventDTO eventDTO, ActivityDTO act) {

    }

    @Override
    protected List<ActivityTaskDTO> getTask(ActivityEventDTO eventDTO, ActivityDTO act) {
        return null;
    }


    @Override
    protected void check(ActivityEventDTO eventDTO, ActivityDTO act) {

    }
}
